<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="居室" prop="houseNum">
        <el-input
          v-model="queryParams.houseNum"
          placeholder="请输入居室"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卫生间" prop="toiletNum">
        <el-input
          v-model="queryParams.toiletNum"
          placeholder="请输入卫生间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="整套面积" prop="houseArea">
        <el-input
          v-model="queryParams.houseArea"
          placeholder="请输入整套面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间号" prop="roomCode">
        <el-input
          v-model="queryParams.roomCode"
          placeholder="请输入房间号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房屋面积" prop="roomArea">
        <el-input
          v-model="queryParams.roomArea"
          placeholder="请输入房屋面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="朝向" prop="direction">
        <el-input
          v-model="queryParams.direction"
          placeholder="请输入朝向"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租金" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入租金"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼层" prop="floor">
        <el-input
          v-model="queryParams.floor"
          placeholder="请输入楼层"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="起租日期" prop="startDate">
        <el-date-picker clearable
          v-model="queryParams.startDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择起租日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="房源描述" prop="introduce">
        <el-input
          v-model="queryParams.introduce"
          placeholder="请输入房源描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房东姓名" prop="ownerName">
        <el-input
          v-model="queryParams.ownerName"
          placeholder="请输入房东姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房东电话" prop="owerPhone">
        <el-input
          v-model="queryParams.owerPhone"
          placeholder="请输入房东电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小区id" prop="villageId">
        <el-input
          v-model="queryParams.villageId"
          placeholder="请输入小区id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小区名称" prop="villageName">
        <el-input
          v-model="queryParams.villageName"
          placeholder="请输入小区名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="具体地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入具体地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房牌号" prop="houseNo">
        <el-input
          v-model="queryParams.houseNo"
          placeholder="请输入房牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布人id" prop="publishId">
        <el-input
          v-model="queryParams.publishId"
          placeholder="请输入发布人id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="0：待出租，1：已出租,2:下架" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入0：待出租，1：已出租,2:下架"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="createName">
        <el-input
          v-model="queryParams.createName"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="updateName">
        <el-input
          v-model="queryParams.updateName"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="houseHall">
        <el-input
          v-model="queryParams.houseHall"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="faceUrl">
        <el-input
          v-model="queryParams.faceUrl"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="decoration">
        <el-input
          v-model="queryParams.decoration"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="agentName">
        <el-input
          v-model="queryParams.agentName"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="agentPhone">
        <el-input
          v-model="queryParams.agentPhone"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="agentUserId">
        <el-input
          v-model="queryParams.agentUserId"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="agentAvatar">
        <el-input
          v-model="queryParams.agentAvatar"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="longitude">
        <el-input
          v-model="queryParams.longitude"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="latitude">
        <el-input
          v-model="queryParams.latitude"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['house:room:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['house:room:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['house:room:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['house:room:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="roomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" />
      <el-table-column label="0整租/1合租" align="center" prop="type" />
      <el-table-column label="居室" align="center" prop="houseNum" />
      <el-table-column label="卫生间" align="center" prop="toiletNum" />
      <el-table-column label="整套面积" align="center" prop="houseArea" />
      <el-table-column label="1主卧,1次卧" align="center" prop="roomType" />
      <el-table-column label="房间号" align="center" prop="roomCode" />
      <el-table-column label="房屋面积" align="center" prop="roomArea" />
      <el-table-column label="朝向" align="center" prop="direction" />
      <el-table-column label="租金" align="center" prop="price" />
      <el-table-column label="楼层" align="center" prop="floor" />
      <el-table-column label="楼房类型" align="center" prop="stepType" />
      <el-table-column label="起租日期" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="房源描述" align="center" prop="introduce" />
      <el-table-column label="房东姓名" align="center" prop="ownerName" />
      <el-table-column label="房东电话" align="center" prop="owerPhone" />
      <el-table-column label="小区id" align="center" prop="villageId" />
      <el-table-column label="小区名称" align="center" prop="villageName" />
      <el-table-column label="具体地址" align="center" prop="address" />
      <el-table-column label="房牌号" align="center" prop="houseNo" />
      <el-table-column label="付款方式" align="center" prop="payType" />
      <el-table-column label="发布人id" align="center" prop="publishId" />
      <el-table-column label="0：待出租，1：已出租,2:下架" align="center" prop="state" />
      <el-table-column label="${comment}" align="center" prop="createName" />
      <el-table-column label="${comment}" align="center" prop="updateName" />
      <el-table-column label="${comment}" align="center" prop="remark" />
      <el-table-column label="${comment}" align="center" prop="houseHall" />
      <el-table-column label="${comment}" align="center" prop="code" />
      <el-table-column label="${comment}" align="center" prop="faceUrl" />
      <el-table-column label="${comment}" align="center" prop="decoration" />
      <el-table-column label="${comment}" align="center" prop="agentName" />
      <el-table-column label="${comment}" align="center" prop="agentPhone" />
      <el-table-column label="${comment}" align="center" prop="agentUserId" />
      <el-table-column label="${comment}" align="center" prop="agentAvatar" />
      <el-table-column label="${comment}" align="center" prop="longitude" />
      <el-table-column label="${comment}" align="center" prop="latitude" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['house:room:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['house:room:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改房源详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="居室" prop="houseNum">
          <el-input v-model="form.houseNum" placeholder="请输入居室" />
        </el-form-item>
        <el-form-item label="卫生间" prop="toiletNum">
          <el-input v-model="form.toiletNum" placeholder="请输入卫生间" />
        </el-form-item>
        <el-form-item label="整套面积" prop="houseArea">
          <el-input v-model="form.houseArea" placeholder="请输入整套面积" />
        </el-form-item>
        <el-form-item label="房间号" prop="roomCode">
          <el-input v-model="form.roomCode" placeholder="请输入房间号" />
        </el-form-item>
        <el-form-item label="房屋面积" prop="roomArea">
          <el-input v-model="form.roomArea" placeholder="请输入房屋面积" />
        </el-form-item>
        <el-form-item label="朝向" prop="direction">
          <el-input v-model="form.direction" placeholder="请输入朝向" />
        </el-form-item>
        <el-form-item label="租金" prop="price">
          <el-input v-model="form.price" placeholder="请输入租金" />
        </el-form-item>
        <el-form-item label="楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入楼层" />
        </el-form-item>
        <el-form-item label="起租日期" prop="startDate">
          <el-date-picker clearable
            v-model="form.startDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择起租日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="房源描述" prop="introduce">
          <el-input v-model="form.introduce" placeholder="请输入房源描述" />
        </el-form-item>
        <el-form-item label="房东姓名" prop="ownerName">
          <el-input v-model="form.ownerName" placeholder="请输入房东姓名" />
        </el-form-item>
        <el-form-item label="房东电话" prop="owerPhone">
          <el-input v-model="form.owerPhone" placeholder="请输入房东电话" />
        </el-form-item>
        <el-form-item label="小区id" prop="villageId">
          <el-input v-model="form.villageId" placeholder="请输入小区id" />
        </el-form-item>
        <el-form-item label="小区名称" prop="villageName">
          <el-input v-model="form.villageName" placeholder="请输入小区名称" />
        </el-form-item>
        <el-form-item label="具体地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入具体地址" />
        </el-form-item>
        <el-form-item label="房牌号" prop="houseNo">
          <el-input v-model="form.houseNo" placeholder="请输入房牌号" />
        </el-form-item>
        <el-form-item label="发布人id" prop="publishId">
          <el-input v-model="form.publishId" placeholder="请输入发布人id" />
        </el-form-item>
        <el-form-item label="0：待出租，1：已出租,2:下架" prop="state">
          <el-input v-model="form.state" placeholder="请输入0：待出租，1：已出租,2:下架" />
        </el-form-item>
        <el-form-item label="${comment}" prop="createName">
          <el-input v-model="form.createName" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="updateName">
          <el-input v-model="form.updateName" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="houseHall">
          <el-input v-model="form.houseHall" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="code">
          <el-input v-model="form.code" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="faceUrl">
          <el-input v-model="form.faceUrl" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="decoration">
          <el-input v-model="form.decoration" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="agentName">
          <el-input v-model="form.agentName" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="agentPhone">
          <el-input v-model="form.agentPhone" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="agentUserId">
          <el-input v-model="form.agentUserId" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="agentAvatar">
          <el-input v-model="form.agentAvatar" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRoom, getRoom, delRoom, addRoom, updateRoom } from "@/api/house/room";

export default {
  name: "Room",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房源详情表格数据
      roomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        houseNum: null,
        toiletNum: null,
        houseArea: null,
        roomType: null,
        roomCode: null,
        roomArea: null,
        direction: null,
        price: null,
        floor: null,
        stepType: null,
        startDate: null,
        introduce: null,
        ownerName: null,
        owerPhone: null,
        villageId: null,
        villageName: null,
        address: null,
        houseNo: null,
        payType: null,
        publishId: null,
        state: null,
        createName: null,
        updateName: null,
        houseHall: null,
        code: null,
        faceUrl: null,
        decoration: null,
        agentName: null,
        agentPhone: null,
        agentUserId: null,
        agentAvatar: null,
        longitude: null,
        latitude: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "0整租/1合租不能为空", trigger: "change" }
        ],
        faceUrl: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询房源详情列表 */
    getList() {
      this.loading = true;
      listRoom(this.queryParams).then(response => {
        this.roomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        houseNum: null,
        toiletNum: null,
        houseArea: null,
        roomType: null,
        roomCode: null,
        roomArea: null,
        direction: null,
        price: null,
        floor: null,
        stepType: null,
        startDate: null,
        introduce: null,
        ownerName: null,
        owerPhone: null,
        villageId: null,
        villageName: null,
        address: null,
        houseNo: null,
        payType: null,
        publishId: null,
        state: null,
        createTime: null,
        createName: null,
        updateTime: null,
        updateName: null,
        remark: null,
        houseHall: null,
        code: null,
        faceUrl: null,
        decoration: null,
        agentName: null,
        agentPhone: null,
        agentUserId: null,
        agentAvatar: null,
        longitude: null,
        latitude: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加房源详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRoom(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改房源详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRoom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRoom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除房源详情编号为"' + ids + '"的数据项？').then(function() {
        return delRoom(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('house/room/export', {
        ...this.queryParams
      }, `room_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
